﻿Public Partial Class CauseOfDelay
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not (IsPostBack) Then
            Dim dtDelay As DataTable = clsGet.Get_CauseOfDelay()
            With ddlDelay
                .ClearSelection()
                .Items.Clear()
                .DataTextField = "Cause_Delay"
                .DataValueField = "Cause_Code"
                .DataSource = dtDelay
                .DataBind()
                .Items.Insert(0, "<--SELECT-->")
                .Items(0).Value = "0"
            End With
            If Not (Session("DelayEdit") Is Nothing) Then
                ViewState("RowEdit") = Session("DelayEdit")
                Dim dt As DataTable = Session("Delay")
                txtStart.Text = dt.Rows(ViewState("RowEdit")).Item("Date_From")
                txtEnd.Text = dt.Rows(ViewState("RowEdit")).Item("Date_To")
                txtTimeStart.Text = dt.Rows(ViewState("RowEdit")).Item("Time_From")
                txtTimeEnd.Text = dt.Rows(ViewState("RowEdit")).Item("Time_To")
                If Not (ddlDelay.Items.FindByValue(dt.Rows(ViewState("RowEdit")).Item("Cause_Code")) Is Nothing) Then
                    ddlDelay.SelectedValue = dt.Rows(ViewState("RowEdit")).Item("Cause_Code")
                End If
                'txtCauseOfDelay.Text = dt.Rows(ViewState("RowEdit")).Item("Cause_Delay")
                'txtDelayTime.Text = dt.Rows(ViewState("RowEdit")).Item("Delay_Time")
            Else
                ViewState("RowEdit") = ""
            End If
        End If

    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (txtStart.Text.Trim() = "") Or (txtEnd.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุวันที่เริ่มต้นและวันที่สิ้นสุด !!');", True)
            Exit Sub
        ElseIf (CDate(ClassFunctionGetData.CDateT(txtStart.Text)) > CDate(ClassFunctionGetData.CDateT(txtEnd.Text))) Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ระบุวันที่ไม่ถูกต้อง !!');", True)
            Exit Sub
        End If
        If (txtTimeStart.Text.Trim() = "") Or (txtTimeEnd.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุเวลาเริ่มต้นและเวลาสิ้นสุด !!');", True)
            Exit Sub
        End If
        If (CDate(ClassFunctionGetData.CDateT(txtStart.Text)) = CDate(ClassFunctionGetData.CDateT(txtEnd.Text))) Then
            If (CInt(txtTimeStart.Text.Substring(0, 2)) > CInt(txtTimeEnd.Text.Substring(0, 2))) Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ระบุเวลาไม่ถูกต้อง !!');", True)
                Exit Sub
            ElseIf (CInt(txtTimeStart.Text.Substring(0, 2)) = CInt(txtTimeEnd.Text.Substring(0, 2))) Then
                If (CInt(txtTimeStart.Text.Substring(3, 2) >= CInt(txtTimeEnd.Text.Substring(3, 2)))) Then
                    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ระบุเวลาไม่ถูกต้อง !!');", True)
                    Exit Sub
                End If
            End If
        End If
        If (ddlDelay.SelectedValue = "0") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุสาเหตุการเกิด Delay !!');", True)
            Exit Sub
        End If
        'If (txtDelayTime.Text.Trim() = "") Then
        '    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุจำนวนเวลาที่ Delay !!');", True)
        '    Exit Sub
        'End If

        Dim Hrs As String = ""
        'If (CInt(txtTimeStart.Text.Substring(3, 2)) < CInt(txtTimeEnd.Text.Substring(3, 2))) Then
        '    Hrs = (CInt(txtTimeEnd.Text.Substring(0, 2)) - CInt(txtTimeStart.Text.Substring(0, 2))).ToString("00") & ":" & (CInt(txtTimeEnd.Text.Substring(3, 2)) - CInt(txtTimeStart.Text.Substring(3, 2))).ToString("00")
        'Else
        '    Dim Hour As Integer = ((CInt(txtTimeEnd.Text.Substring(0, 2)) - CInt(txtTimeStart.Text.Substring(0, 2))) - 1)
        '    Dim Minute As Integer = (60 - CInt(txtTimeStart.Text.Substring(3, 2))) + CInt(txtTimeEnd.Text.Substring(3, 2))
        '    If (Minute = 60) Then
        '        Minute = 0
        '        Hour += 1
        '    End If
        '    Hrs = Hour.ToString("00") & ":" & Minute.ToString("00")
        'End If

        Dim dtStart As String = DateTime.Parse(txtStart.Text & " " & txtTimeStart.Text)
        Dim dtEnd As String = DateTime.Parse(txtEnd.Text & " " & txtTimeEnd.Text)


        Dim ts = DateDiff("n", dtStart, dtEnd) ' in case you need it in minutes

        Hrs = (ts \ 60).ToString("00") & ":" & (ts Mod 60).ToString("00")
        '  ts = ts * 2
        '   ts = ts \ 60
        '  ts = Format(ts, "00.00")




        Dim dt As DataTable = Session("Delay")
        If (ViewState("RowEdit") & "" = "") Then
            'ใหม่
            Dim dr As DataRow = dt.NewRow
            dr.Item("Delay_DateTime") = txtStart.Text & " " & txtTimeStart.Text & " - " & txtEnd.Text & " " & txtTimeEnd.Text
            dr.Item("Cause_Delay") = ddlDelay.SelectedItem.Text
            dr.Item("Cause_Code") = ddlDelay.SelectedValue
            dr.Item("Delay_Time") = Hrs
            dr.Item("Date_From") = txtStart.Text
            dr.Item("Date_To") = txtEnd.Text
            dr.Item("Time_From") = txtTimeStart.Text
            dr.Item("Time_To") = txtTimeEnd.Text
            dt.Rows.Add(dr)
            dt.AcceptChanges()
        Else
            'เก่า
            Dim index As Integer = ViewState("RowEdit")
            dt.Rows(index).Item("Delay_DateTime") = txtStart.Text & " " & txtTimeStart.Text & " - " & txtEnd.Text & " " & txtTimeEnd.Text
            dt.Rows(index).Item("Cause_Delay") = ddlDelay.SelectedItem.Text
            dt.Rows(index).Item("Cause_Code") = ddlDelay.SelectedValue
            dt.Rows(index).Item("Delay_Time") = Hrs
            dt.Rows(index).Item("Date_From") = txtStart.Text
            dt.Rows(index).Item("Date_To") = txtEnd.Text
            dt.Rows(index).Item("Time_From") = txtTimeStart.Text
            dt.Rows(index).Item("Time_To") = txtTimeEnd.Text
            dt.AcceptChanges()
        End If
        Session("Delay") = dt
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModalPopup('S');", True)
    End Sub

    Private Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModalPopup('N');", True)
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "")
        End If
    End Sub
End Class